/**
  ******************************************************************************
  * @file    bsp_ble.c
  * @author  Iron
  * @date    2018-07-19
  * @version v1.0
  * @brief   bsp ble c file
  */

/** @addtogroup GROUP_BSP
  * @{
  */

/* includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "bsp_ble.h"
#include "bsp_usart_ex.h"

/* private typedef -----------------------------------------------------------*/
/* private macro -------------------------------------------------------------*/
// gpio config
#define BLE_LINK_STATUS_PIN   GPIO_PIN_9  // BLE_STATUS  PC9  - 66
#define BLE_LINK_STATUS_PORT  GPIOC
#define BLE_LINK_STATUS()     HAL_GPIO_ReadPin(BLE_LINK_STATUS_PORT, BLE_LINK_STATUS_PIN)
#define BLE_LINK_STATUS_PORT_RCC_INIT()  __HAL_RCC_GPIOC_CLK_ENABLE()
#define BLE_GET_LINK_STATUS() HAL_GPIO_ReadPin(BLE_LINK_STATUS_PORT, BLE_LINK_STATUS_PIN)

// uart config
#define BLE_USART  BSP_USART6

/* private variables ---------------------------------------------------------*/
static bsp_usart_handle_t ble_usart;

/* private function prototypes -----------------------------------------------*/
/* private functions ---------------------------------------------------------*/
void bsp_ble_usart_ex_isr_rx_indicate(void *handle, uint32_t length, BaseType_t *pxHigherPriorityTaskWoken);
void bsp_ble_usart_ex_isr_tx_complete(void *handle, BaseType_t *pxHigherPriorityTaskWoken);

int32_t bsp_ble_gpio_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    BLE_LINK_STATUS_PORT_RCC_INIT();

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Pin = BLE_LINK_STATUS_PIN;
    HAL_GPIO_Init(BLE_LINK_STATUS_PORT, &GPIO_InitStruct);

    return 0;
}

int32_t ble_is_link(void)
{
    return BLE_GET_LINK_STATUS();
}

__weak void bsp_ble_usart_ex_isr_rx_indicate(void *handle, uint32_t length, BaseType_t *pxHigherPriorityTaskWoken) {}
__weak void bsp_ble_usart_ex_isr_tx_complete(void *handle, BaseType_t *pxHigherPriorityTaskWoken) {}

int32_t bsp_ble_uart_init(void)
{
    return bsp_usart_ex_init(&ble_usart, BLE_USART, 9600, bsp_ble_usart_ex_isr_rx_indicate, bsp_ble_usart_ex_isr_tx_complete);
}

int32_t ble_uart_send(uint8_t *data, int32_t len, uint32_t timeout)
{
    return bsp_usart_send_ex(&ble_usart, data, len, timeout);
}

// The function caller take the responsibility to keep "data" safe!
int32_t ble_uart_send_no_wait(uint8_t *data, int32_t len)
{
    return bsp_usart_send_tx_no_wait(&ble_usart, data, len);
}

int32_t ble_uart_getbyte(uint8_t *data, uint32_t timeout)
{
    return bsp_usart_getbyte_ex(&ble_usart, data, timeout);
}

int32_t ble_uart_recv(uint8_t *data, int32_t size, uint32_t timeout)
{
    return bsp_usart_recv_ex(&ble_usart, data, size, timeout);
}

int bsp_ble_init(void)
{
    bsp_ble_gpio_init();
    bsp_ble_uart_init();

    return 0;
}

/**
* @}
*/

/******************* (C)COPYRIGHT 2018 ***** END OF FILE *********************/
